<!-- 编辑性别组件 -->
<template>
  <div>
    <van-picker title="更新性别" show-toolbar :columns="columns" @confirm="onConfirm" @cancel="onCancel"
      :default-index="gender" />
  </div>
</template>

<script>
import { setUserprofile } from '@/api/user.js'
export default {
  data () {
    return {
      // 选择器数据
      columns: ['男', '女']
    }
  },
  props: {
    // 选中的性别
    gender: {
      type: Number,
      require: true
    }
  },
  model: {
    prop: 'gender'
  },
  methods: {
    // 确认
    async onConfirm (val, index) {
      this.$toast.loading({
        message: '修改中...',
        forbidClick: true,
        duration: 0
      })
      try {
        await setUserprofile({
          gender: index
        })
        this.$toast('修改性别成功')
        // 关闭弹出层
        this.$emit('closePopup')
        // 同步视图
        this.$emit('input', index)
      } catch (err) {
        this.$toast('修改性别失败')
      }
    },
    // 取消
    onCancel () {
      this.$emit('closePopup')
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
